package com.info.zhiduoduo.repository.mapper.order;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.product.OrderProductWxDto;
import com.info.zhiduoduo.repository.dto.product.OrderUserInfoDto;
import com.info.zhiduoduo.repository.entity.order.OrderProduct;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 购买AI助手智小多订单 Mapper 接口
 *
 * @author fanlei
 * @since 2023-07-04
 */
public interface OrderProductMapper extends BaseMapper<OrderProduct> {

    @Select("select * from zdd_order_product where orderno = #{orderno} limit 1")
    OrderProduct getOneOrderProduct(@Param("orderno") String orderno);

    @Select("select * from zdd_order_product where userid = #{userid} and state = '2'")
    List<OrderProductWxDto> listOrderByUser(@Param("userid") Integer userid);

    @Select("select * from zdd_order_product where take_state = '1' and state = '2' and notice_flag = '0'")
    List<OrderProduct> listOrderFroNotice();

    /**
     * 返回手机号和姓名脱敏
     *
     * @return
     */
    @Select(" SELECT " +
            " concat( LEFT ( ma.realname, 1 ), '**' ) realName, " +
            " concat( LEFT ( op.phone_no, 3 ), '****', RIGHT ( op.phone_no, 4 ) ) phoneNo " +
            " FROM" +
            " zdd_order_product op " +
            " LEFT JOIN zdd_maker_account ma ON op.userid = ma.userid " +
            " WHERE" +
            " op.state = '2' " +
            " ORDER BY " +
            " op.createtime DESC " +
            " LIMIT 20")
    List<OrderUserInfoDto> listOrderUserInfo();

    @Select(" SELECT " +
            "  op.*," +
            "  ma.realname realName " +
            " FROM " +
            "  zdd_order_product op" +
            "  LEFT JOIN zdd_maker_account ma ON op.userid = ma.userid " +
            " WHERE " +
            "  state = '2'" +
            " AND IF(ISNULL(#{phoneNo}) or #{phoneNo}='',1=1, op.phone_no = #{phoneNo}) " +
            " AND IF(ISNULL(#{takeState}) or #{takeState}='', 1=1 , op.take_state = #{takeState}  )" +
            " ORDER BY" +
            " take_state ASC," +
            " paytime DESC")
    List<OrderProductWxDto> listOrderAll(@Param("phoneNo") String phoneNo, @Param("takeState") String takeState);
}
